app.register.controller('RenewalCtrl', function($scope, $http, $rootScope,
		$route, RenwalSer, AppUtil, $routeParams) {
	
	loadFlag($routeParams.id,$scope);
	
	// 获取本地存储中的页码页数 tableID+page or size
	var page = AppUtil.GetLocalValue("RenewalTable_Page");
	var size = AppUtil.GetLocalValue("RenewalTable_Size");
	$scope.pagination = {
			totalItems :0,
		currentPage : page == undefined ? 1 : page,
		itemsPerPage : size == undefined ? 10 : size
	};
	var date = new Date();
	var nowYear = date.getFullYear();
    var month=date.getMonth()+1>=10?date.getMonth()+1:'0'+(date.getMonth()+1);
	$("#report_year").val(nowYear);
	$("#report_month").val(month);
	$scope.SearchData = function() {
		$scope.postData = {};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		if($("#entity_code").val()=="")
		{
			alertMsg("提示","请选择店铺信息.");
			return;
		}
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		
		if ($scope.postData == undefined)
			$scope.postData = {};
		// 表单查询参数
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, params,$scope.selected);
		
		if(!params.entity_code)
			return;
		params.month=params.report_year+'-'+params.report_month;
		// 当有查询参数时,重置页数
		
		$scope.postData = $.extend({}, $scope.postData,params);
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		AppUtil.loading();
		RenwalSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			if(response.code==-1){
				alertMsg("提示",response.message);
			}
			$scope.pagination.totalItems = response.total;
			$scope.datas = response.data.datas;
			$scope.t = response.data.total;
		}).error(function(response){
			alertMsg("提示",'系统错误,清稍后重试.');
		});
	}

	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);

	// 加载查询信息
	AppUtil.LoadGroups();
	// 导出
	$scope.ExportData = function() {
		if($("#entity_code").val()=="")
		{
			alertMsg('提示',"请选择店铺信息.");
			return;
		}
		var data = {};
		data.params=AppUtil.Params(".form_params");
		data.params.month=data.params.report_year+'-'+data.params.report_month;
		AppUtil.ExportData(data);
	};
	
	
	$scope.LoadEmps=function(entity){
		
		$http.get("perfo/emps?entity_code="+entity.entity_code+"&emp_type=is_insurance_advisor&isValid=1").success(function(data) {
			$("#emps").html("");
			$.each(data,function(i,o){
				$("#emps").append("<option value='"+o.emp_no+"'>"+o.emp_name+"</option>")
			});
			$("#emps").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#empsDiv").show();
			$("#emps").multiselect('refresh');
			
		});	
		
		
	};


});

app.register.service('RenwalSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('perfo/renewal', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};

});
